﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageUser
{
    public partial class PageChangePass : System.Web.UI.Page
    {
        private CThongTinCaNhan _cttcn = new CThongTinCaNhan();
        private bool _flag = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            lbThongBao.Visible = false;
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (_flag)
            {
                int idtaikhoan = Convert.ToInt32(Session["IDDN"].ToString());
                string matkhau = txtNewPass.Text.Trim();
                bool kq = _cttcn.UpdatePassword(idtaikhoan, matkhau);
                lbThongBao.Visible = true;
                if (kq)
                {
                    pnThongBao.CssClass = "InRight success message";
                    lbThongBao.Text = "Update new password success.";
                }
                else
                {
                    pnThongBao.CssClass = "InRight error message";
                    lbThongBao.Text = "Update new password error.";
                }
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtNewPass.Text = "";
            txtOldPass.Text = "";
            txtPrePass.Text = "";
            lbThongBao.Visible = false;
        }

        protected void cvPrePassword_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (txtNewPass.Text.Equals(txtPrePass.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
                _flag = false;
            }
        }

        protected void cvOldPass_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (!Session["IDDN"].ToString().Equals(""))
            {
                int idtaikhoan = Convert.ToInt32(Session["IDDN"].ToString());
                string matkhau = txtOldPass.Text.Trim();
                bool kq = _cttcn.CheckPassword(idtaikhoan, matkhau);
                if (kq)
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                    _flag = false;
                }
            }
        }
    }
}